import os
from conans import ConanFile, tools
from conans.errors import ConanInvalidConfiguration
import sys
sys.path.append(os.environ.get('PYTHONPATH'))
from pathlib import Path

class StrawberryperlConan(ConanFile):
    name = "strawberryperl"
    version = "5.30.0.1"

    description = "Strawbery Perl for Windows. Useful as build_require"
    license = "GNU Public License or the Artistic License"
    homepage = "http://strawberryperl.com"
    url = "https://caiyu-conan-1251974573.cos.ap-nanjing.myqcloud.com/strawberry-perl-5.32.0.1-32bit-portable.zip"
    topics = ("conan", "installer", "perl", "windows")
    settings = "os_build", "arch_build"
    def source(self):
        print("source  cwd is", os.getcwd())
        print("sys path is", sys.path)
        my_file = Path("strawberry-perl-5.32.0.1-32bit-portable.zip")
        print(my_file.exists())
        if my_file.exists() == False:
            tools.download(self.url,"strawberry-perl-5.32.0.1-32bit-portable.zip")
            print("shits")
    def configure(self):
        if self.settings.os_build != "Windows":
            raise ConanInvalidConfiguration("Only windows supported for Strawberry Perl.")

    def build(self):
        tools.unzip("strawberry-perl-5.32.0.1-32bit-portable.zip","")

    def package(self):
        self.copy(pattern="License.rtf*", dst="licenses", src="licenses")
        self.copy(pattern="*", src=os.path.join("perl", "bin"), dst="bin")
        self.copy(pattern="*", src=os.path.join("perl", "lib"), dst="lib")
        self.copy(pattern="*", src=os.path.join("perl", "vendor", "lib"), dst="lib")
        self.copy(pattern="*", src=os.path.join("c", "bin"), dst="bin")
        self.copy(pattern="*", src=os.path.join("c", "lib"), dst="lib")
        self.copy(pattern="*", src=os.path.join("c", "include"), dst="include")
        tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig"))

    def package_info(self):
        bin_path = os.path.join(self.package_folder, "bin")
        self.output.info("Appending PATH environment variable: %s" % bin_path)
        self.env_info.PATH.append(bin_path)

